<template>
	<view class="container">
		<view class="content">
			<view class="title">上传打卡照片</view>
			<view class="imgbox" @click="upPhoto">
				<block v-if="imgUrl == ''">
					<image class="icon"
						src="https://zrws-1327345771.cos.ap-shanghai.myqcloud.com/images/1733739708026椭圆 1@2x.png"
						mode=""></image>
					<view class="uptips">上传打卡图片</view>
					<view class="djup">点击上传</view>
				</block>
				<image v-else :src="imgUrl" class="upimg" mode="widthFix"></image>
			</view>
			<view class="btnbox">
				<view class="left" @click="tocancel">取消</view>
				<view class="right" @click="toSubmit">提交</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		submitTask
	} from "@/api/yanxue/user";
	export default {
		data() {
			return {
				imgUrl: ''
			}
		},
		onLoad(options) {
			this.id = options.id
		},
		methods: {
			upPhoto() {
				const app = this
				uni.chooseImage({
					count: 1, // 设置最多选择9张图片
					sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"],
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading()
						uni.uploadFile({
							url: "https://yxapi.ziranweishi.com.cn/font/uploadByOss",
							filePath: tempFilePaths[0],
							name: "file",
							success: (uploadFileRes) => {
								uni.hideLoading()
								const res = JSON.parse(uploadFileRes.data);
								app.imgUrl = res.data
							},
						});
					},
					fail: (err) => {
						uni.hideLoading()
						console.log("err :>> ", err);
					},
				});
			},
			tocancel() {
				uni.navigateBack({
					delta: 1
				})
			},
			toSubmit() {
				if(this.imgUrl ==''){
					return uni.showToast({
						title:'请先上传图片',
						icon:'none'
					})
				}
				let obj = {
					content: "",
					detailImg: this.imgUrl,
					taskId: this.id
				}
				submitTask(obj).then(res => {
					uni.showToast({
						title:'任务打卡成功',
						icon: 'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1000)
				})
			}
		}
	};
</script>
<style scoped lang="scss">
	.container {
		width: 750rpx;
		height: 100vh;
		overflow: scroll;
		background: linear-gradient(0deg, #C1F26C 0%, #FFFFFF 100%);

		.content {
			width: 702rpx;
			height: 1310rpx;
			background: #FFFFFF;
			box-shadow: -4rpx 0rpx 19rpx 1rpx rgba(52, 52, 52, 0.15);
			border-radius: 48rpx;
			margin: 30rpx auto;
			overflow: hidden;

			.title {
				width: 190rpx;
				font-family: HarmonyOS Sans SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #111827;
				line-height: 52rpx;
				margin: 30rpx;
			}

			.imgbox {
				width: 642rpx;
				height: 858rpx;
				background: #FFFFFF;
				border-radius: 48rpx;
				border: 4px solid #F95513;
				margin: 30rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.upimg {
					width: 600rpx;
					height: 800rpx;
					border-radius: 30rpx;
				}

				.icon {
					width: 132rpx;
					height: 110rpx;
					margin: 0rpx 0 30rpx;
				}

				.uptips {
					font-family: HarmonyOS Sans SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #F95513;
					line-height: 41rpx;
				}

				.djup {
					font-family: HarmonyOS Sans SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #718096;
					line-height: 36rpx;
					margin-top: 16rpx;
				}
			}

			.btnbox {
				margin-top: 168rpx;
				width: 100%;
				display: flex;
				justify-content: space-between;
				padding: 0 28rpx;

				.left {
					width: 308rpx;
					height: 107rpx;
					border-radius: 48rpx;
					border: 6rpx solid #F95513;
					line-height: 96rpx;
					font-family: HarmonyOS Sans SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #F95513;
					text-align: center;
				}

				.right {
					width: 308rpx;
					height: 107rpx;
					border-radius: 48rpx;
					background: #F95513;
					line-height: 107rpx;
					font-family: HarmonyOS Sans SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #FFF;
					text-align: center;
				}
			}
		}
	}
</style>